﻿using System;
using System.Text;
using System.Data;
using System.Collections;
using WP.RM.DataEntity;
using WP.RM.DataAccess;
using WP.RM.BusinessRule;
using WP.DB;
using WP.Commons;

namespace WP.RM.BusinessLogical
{
    public class RMProductBOMHdrBLL : IBusinessLogical
    {
        RMProductBOMHdrDAL rmProductBOMDAL = new RMProductBOMHdrDAL();
        #region For implement interface

        #region Select Record by RECID OverLoad +1
        public RMProductBOMHdrDEL  SelectDataByRecID(DBAccess dBAccess, string strRecId)
        {
            RMProductBOMHdrDEL rmProductBOMRevDEL = new RMProductBOMHdrDEL();

            DataFunctions.SelectByRecID(dBAccess, rmProductBOMRevDEL, rmProductBOMDAL.Fields, strRecId);

            return rmProductBOMRevDEL;
        }
        public IDataEntity SelectDataByRecID(string strRecId)
        {
            DBAccess dBAccess = new DBAccess(false, false);
            RMProductBOMHdrDEL rmProductBOMRevDEL = null;

            try
            {
                rmProductBOMRevDEL = SelectDataByRecID(dBAccess, strRecId);
                return rmProductBOMRevDEL as IDataEntity;
            }
            catch (Exception e)
            {
                ExceptionManager.ProcessException(e);
                return null;
            }
            finally
            {
                dBAccess.CloseCon();
            }
        }
        #endregion

        #region public bool Insert()
        public bool Insert(IDataEntity iRMProductBOMRevDEL)
        {
            bool bReturn = true;
            RMProductBOMHdrDEL rmProductBOMRevDEL = iRMProductBOMRevDEL as RMProductBOMHdrDEL;
            DBAccess dBAccess = null;

            dBAccess = new DBAccess(false, true);
            try
            {
                bReturn = Insert(dBAccess, rmProductBOMRevDEL);

                if (bReturn)
                {
                    dBAccess.CommitTranAndCloseCon();
                }
                else
                {
                    dBAccess.RollbackTranAndClose();
                    if (SysVar.ErrorMessage.Count > 0)
                    {
                        MsgBoxEx.Show(SysVar.ErrorMessage, SysVar.WarnTitle);
                    }
                }
            }
            catch (Exception e)
            {
                dBAccess.RollbackTranAndClose();
                ExceptionManager.ProcessException(e);
                bReturn = false;
            }
            return bReturn;
        }
        public bool Insert(DBAccess dBAccess, RMProductBOMHdrDEL rmProductBOMRevDEL)
        {
            bool bFlag = true;
            RMProductBOMHdrBRL  rmProductBOMRevBRL = new RMProductBOMHdrBRL();

            if (rmProductBOMRevBRL.InsertValid(dBAccess, rmProductBOMRevDEL))
            {
                DataFunctions.InsertTable(dBAccess, rmProductBOMDAL.Fields, rmProductBOMRevDEL);
            }
            else
            {
                bFlag = false;
            }
            return bFlag;
        }
        #endregion

        #region public bool Delete()
        public bool Delete(string strRecId, byte[] btOldRecStamp)
        {
            DBAccess dBAccess = new DBAccess(false, true);
            bool bFlag = false;

            try
            {
                if (new RMProductBOMHdrBRL().DeleteValid(dBAccess, strRecId, btOldRecStamp))
                {
                    string strSQL = "select * from rmProductBOMHdr where RecID='" + strRecId + "'";
                    DataRow drRow = dBAccess.GetDataFirstRow(strSQL);
                    if (drRow != null)
                    {
                        string strCategoryName = CvtFunc.ToString(drRow["CategoryName"]);
                        string strSetName = CvtFunc.ToString(drRow["SetName"]);
                        string strProductName = CvtFunc.ToString(drRow["ProductName"]);
                        string strVersionNo = CvtFunc.ToString(drRow["VersionNo"]);
                        string strTechnicsName = CvtFunc.ToString(drRow["TechnicsName"]);  

                        RMProductBOMHdrDAL rmDAL = new RMProductBOMHdrDAL();
                        strSQL = " select RecID from dbo.rmProductBOMDetail where CategoryName='{0}' and SetName='{1}' and ProductName='{2}' and VersionNo='{3}' and TechnicsName='{4}'";
                        strSQL = string.Format(strSQL, strCategoryName, strSetName, strProductName, strVersionNo,strTechnicsName);
                        DataTable dtProductBOM = dBAccess.GetDataTable(strSQL);
                        foreach ( DataRow drProductBOM in dtProductBOM.Rows )
                        {   
                            rmDAL.DeleteDetailBOM(dBAccess, CvtFunc.ToString ( drProductBOM["RecID"]) ); 
                        }


                        strSQL = "select RecID from dbo.rmProductBOMLabourCost where CategoryName='{0}' and SetName='{1}' and ProductName='{2}' and VersionNo='{3}' and TechnicsName='{4}'";
                        strSQL = string.Format(strSQL, strCategoryName, strSetName, strProductName, strVersionNo,strTechnicsName);
                        DataTable dtBOMLabourCost = dBAccess.GetDataTable(strSQL);
                        foreach (DataRow drBOMLabourCost in dtBOMLabourCost.Rows)
                        {
                            rmDAL.DeleteDetail_LabourCost(dBAccess, CvtFunc.ToString (drBOMLabourCost["RecID"])); 
                        }

                        strSQL = " Delete from dbo.rmProductBOMHdr where CategoryName='{0}' and SetName='{1}' and ProductName='{2}' and VersionNo='{3}' and TechnicsName='{4}'";
                        strSQL = string.Format(strSQL, strCategoryName, strSetName, strProductName, strVersionNo,strTechnicsName);
                        dBAccess.ExecuteSql(strSQL);

                        DataFunctions.DeleteByRecID(dBAccess, "rmProductBOMDetailHdr", strRecId);
                    }
                    dBAccess.CommitTranAndCloseCon();

                    bFlag = true;
                }
                else
                {
                    dBAccess.RollbackTranAndClose();
                    if (SysVar.ErrorMessage.Count > 0)
                    {
                        MsgBoxEx.Show(SysVar.ErrorMessage, SysVar.WarnTitle);
                    }
                }
            }
            catch (Exception e)
            {
                dBAccess.RollbackTranAndClose();
                ExceptionManager.ProcessException(e);
            }

            return bFlag;
        }
        #endregion

        #region public bool Update()
        public bool Update(IDataEntity iProductBOMRevDEL)
        {
            bool bReturn = true;
            RMProductBOMHdrDEL rmProductBOMRevDEL = iProductBOMRevDEL as RMProductBOMHdrDEL;
            DBAccess dBAccess = new DBAccess(false, true);

            try
            {
                bReturn = Update(dBAccess, rmProductBOMRevDEL);
                if (bReturn)
                {
                    dBAccess.CommitTranAndCloseCon();
                }
                else
                {
                    dBAccess.RollbackTranAndClose();
                    if (SysVar.ErrorMessage.Count > 0)
                    {
                        MsgBoxEx.Show(SysVar.ErrorMessage, SysVar.WarnTitle);
                    }
                }
            }
            catch (Exception e)
            {
                dBAccess.RollbackTranAndClose();
                ExceptionManager.ProcessException(e);
                return false;
            }

            return bReturn;
        }
        public bool Update(DBAccess dBAccess, RMProductBOMHdrDEL rmProductBOMRevDEL)
        {
            bool bFlag = true;
            RMProductBOMHdrBRL rmProductBOMRevBRL = new RMProductBOMHdrBRL();

            if (rmProductBOMRevBRL.UpdateValid(dBAccess, rmProductBOMRevDEL))
            {
                DataFunctions.UpdateTable(dBAccess, rmProductBOMDAL.Fields, RMProductBOMHdrDEL.TABLENAME_RMProductBOMHDR, rmProductBOMRevDEL.Tables[0].Rows[0]);
            }
            else
            {
                bFlag = false;
            }
            return bFlag;
        }
        #endregion


        #endregion
    }
}
